package com.wjy.game.viewer;

import com.wjy.game.message.data.res.ListRoomRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wjy
 * @date 2025/5/26 16:18
 * @description 主页视图
 */
@Slf4j
@Component
public class IndexViewer extends ClientViewer{

    private JButton createButton = new JButton("创建房间");

    private JButton refreshButton = new JButton("刷新");

    private JList<ListRoomRes.Room> rooms = new JList<>();

    @Override
    protected void repaintUI() {
        JPanel top = new JPanel();
        top.setBackground(Color.GREEN);
        top.add(createButton);
        top.add(refreshButton);
        top.setPreferredSize(new Dimension(770, 40));

        createButton.setPreferredSize(new Dimension(100, 30));
        refreshButton.setPreferredSize(new Dimension(100, 30));
        add(top, BorderLayout.NORTH);

        JPanel left = new JPanel();
        left.setBackground(Color.MAGENTA);
        add(left, BorderLayout.CENTER);
        left.setPreferredSize(new Dimension(530, 530));
//        left.setBounds(25, 40, 480, 480);
        Box center = Box.createVerticalBox();
        center.setPreferredSize(new Dimension(530, 530));
//        center.setBounds(25, 40, 480, 480);
        JScrollPane scrollPane = new JScrollPane(center, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//        center.add(scrollPane);
//        scrollPane.setBounds(20, 20, 480, 480);
        center.setBackground(Color.BLUE);
        initRooms(center);
        left.add(scrollPane);

        JPanel right = new JPanel();
        add(right, BorderLayout.EAST);
        right.setPreferredSize(new Dimension(240, 530));
//        right.setBounds(530, 40, 230, 530);
        right.setBackground(Color.CYAN);

//        createButton.requestFocus();
//        refreshButton.requestFocus();
    }

    private void initRooms(JComponent center) {
        ListRoomRes res = getRoomList();
        List<ListRoomRes.Room> rooms = res.getRooms();
        if (rooms != null && !rooms.isEmpty()) {
            for (ListRoomRes.Room rm : rooms) {
                JPanel roomPanel = new JPanel();
                roomPanel.setBackground(Color.RED);
                roomPanel.setPreferredSize(new Dimension(480, 40));
                JLabel roomName = new JLabel(rm.getRoomName());
                roomName.setPreferredSize(new Dimension(100, 40));
                roomPanel.add(roomName);
                center.add(roomPanel);
            }
        }
    }

    private ListRoomRes getRoomList() {
        ListRoomRes res = new ListRoomRes();
        List<ListRoomRes.Room> rooms = new ArrayList<>(35);
        for (int i = 0; i < 35; i++) {
            ListRoomRes.Room room = new ListRoomRes.Room();
            room.setRoomId(String.valueOf(i));
            room.setRoomName("房间" + i);
            rooms.add(room);
        }
        res.setRooms(rooms);
        return res;
    }

    @Override
    protected void actions() {
        addBtnListener(refreshButton, e -> {
            log.info("刷新");
            initUI();
        });
    }

    @Override
    protected LayoutManager getLayoutManager() {
        return new BorderLayout();
    }
}
